package com.example.bbang.mapper;

import com.example.bbang.entity.Order;
import com.example.bbang.entity.User;
import org.apache.ibatis.annotations.*;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

public interface OrderMapper {
    @Select("select * from `order` where bname=#{bname} and orderStatus=#{orderStatus} ORDER BY orderid DESC")
    @Transactional
     List<Order> getOrderById(@Param("bname")String bname, @Param("orderStatus")Integer orderStatus);


    @Select("select * from `order` where orderid=#{orderid} ORDER BY orderid DESC")
    @Transactional
    List<Order> getOrderById1(@Param("orderid")Integer orderid);


    @Insert("INSERT INTO `order` ( `username`, `money`,`price`,`orderName`, `buyAddress`, " +
            "`detailAddress`, `img`, `orderCreateTime`, `orderStatus`,`dh`,`type`,`district`) " +
            "VALUES (#{username},#{money},#{price},#{orderName},#{buyAddress},#{detailAddress}," +
            "#{img},#{orderCreateTime},#{orderStatus},#{dh},#{type},#{district});")
    @Transactional
    void addOrderById(Order order);

    @Select("select * from `order` ORDER BY orderid DESC ")    List<Order>  getOrder();

    //分页拿order
    @Select("select * from `order` ORDER BY orderid DESC limit #{pageNum},#{pageSize} ")
    @Transactional
    List<Order> selectPage(@Param("pageNum") Integer pageNum, @Param("pageSize") Integer pageSize);

    //bs分页拿order
    @Select("select * from `order` where orderStatus=#{orderStatus} ORDER BY orderid DESC limit #{pageNum},#{pageSize} ")
    @Transactional
    List<Order> bsselectPage(@Param("pageNum") Integer pageNum, @Param("pageSize") Integer pageSize, @Param("orderStatus") Integer orderStatus);

    //模搜分页
    @Select("select * from `order` where orderName like CONCAT('%',#{orderName},'%') ORDER BY orderid DESC  limit #{pageNum},#{pageSize} ")
    @Transactional
    List<Order> findPageByOrderName(@Param("pageNum") Integer pageNum, @Param("pageSize") Integer pageSize,@Param("orderName") String orderName);

    //bs模搜分页
    @Select("select * from `order` where orderName like CONCAT('%',#{orderName},'%')  and orderStatus=#{orderStatus} ORDER BY orderid DESC limit #{pageNum},#{pageSize} ")
    @Transactional
    List<Order> bsfindPageByOrderName(@Param("pageNum") Integer pageNum, @Param("pageSize") Integer pageSize,@Param("orderName") String orderName, @Param("orderStatus") Integer orderStatus);

    //3
    @Select("select * from `order`  where orderName like CONCAT('%',#{orderName},'%') and orderStatus=#{orderStatus}-1 and username=#{username} ORDER BY orderid DESC limit #{pageNum},#{pageSize} ")
    @Transactional
    List<Order> findPageByOrderStatus(@Param("pageNum") Integer pageNum, @Param("pageSize") Integer pageSize,@Param("orderStatus") Integer orderStatus,@Param("orderName")  String orderName,@Param("username")  String username);



    //bs3
    @Select("select * from `order`  where orderName like CONCAT('%',#{orderName},'%') and orderStatus=#{orderStatus}-1 and bname=#{bname} ORDER BY orderid DESC limit #{pageNum},#{pageSize} ")
    @Transactional
    List<Order> bsfindPageByOrderStatus(@Param("pageNum") Integer pageNum, @Param("pageSize") Integer pageSize,@Param("orderStatus") Integer orderStatus,@Param("orderName")  String orderName,@Param("bname")  String bname);





    //少搜索
    @Select("select * from `order`  where  orderStatus=#{orderStatus}-1 and username=#{username} ORDER BY orderid DESC limit #{pageNum},#{pageSize} ")
    @Transactional
    List<Order> findPageByOrderStatus1(@Param("pageNum") Integer pageNum, @Param("pageSize") Integer pageSize,@Param("orderStatus") Integer orderStatus,@Param("username")  String username);


    //bs少搜索
    @Select("select * from `order`  where  orderStatus=#{orderStatus}-1 and bname=#{bname} ORDER BY orderid DESC limit #{pageNum},#{pageSize} ")
    @Transactional
    List<Order> bsfindPageByOrderStatus1(@Param("pageNum") Integer pageNum, @Param("pageSize") Integer pageSize,@Param("orderStatus") Integer orderStatus,@Param("bname")  String bname);




    //少status
    @Select("select * from `order`  where orderName like CONCAT('%',#{orderName},'%')  and username=#{username} ORDER BY orderid DESC limit #{pageNum},#{pageSize} ")
    @Transactional
    List<Order> findPageByOrderOrdername(@Param("pageNum") Integer pageNum, @Param("pageSize") Integer pageSize,@Param("orderName")  String orderName,@Param("username")  String username);



    //bs少status
    @Select("select * from `order`  where orderName like CONCAT('%',#{orderName},'%')  and bname=#{bname} and orderStatus > 0 ORDER BY orderid DESC limit #{pageNum},#{pageSize} ")
    @Transactional
    List<Order> bsfindPageByOrderOrdername(@Param("pageNum") Integer pageNum, @Param("pageSize") Integer pageSize,@Param("orderName")  String orderName,@Param("bname")  String bname);



    @Update("<script> " +
            "update `order` " +
            "<set> " +
            "<if test = \"bname != null and bname!=''\">" +
            "bname=#{bname}," +
            "</if>" +
            "<if test = \"money != null and money!=''\">" +
            "money=#{money}," +
            "</if>" +

            "<if test = \"buyAddress != null and buyAddress!=''\">" +
            "buyAddress=#{buyAddress}," +
            "</if>" +
            "<if test = \"detailAddress != null and detailAddress!=''\">" +
            "detailAddress=#{detailAddress}," +
            "</if>" +
            "<if test = \"img != null and img!=''\">" +
            "img=#{img}," +
            "</if>" +
            "<if test = \"orderCreateTime != null and orderCreateTime!=''\">" +
            "orderCreateTime=#{orderCreateTime}," +
            "</if>" +
            "<if test = \"orderStatus != null and orderStatus!=''\">" +
            "orderStatus=#{orderStatus}," +
            "</if>" +
            "<if test = \"orderName != null and orderName!=''\">" +
            "orderName=#{orderName}," +
            "</if>" +
            "<if test = \"dh != null and dh!=''\">" +
            "dh=#{dh}," +
            "</if>" +
            "<if test = \"price != null and price!=''\">" +
            "price=#{price}," +
            "</if>" +
            "<if test = \"type != null and type!=''\">" +
            "type=#{type}," +
            "</if>" +
            "<if test = \"district != null and district!=''\">" +
            "district=#{district}," +
            "</if>" +
            "</set>" +
            " where orderid = #{orderid} " +
            "</script> ")
    void updateById(Order order);

    @Delete("delete from `order` where orderid=#{orderid}")
    void deleteById(Order order);


    @Update("update `order` set orderStatus=#{orderStatus},bname=#{bname} where orderid = #{orderid}  ")
    void updateByName(Order order);

    @Select("select * from `order` where orderStatus=#{orderStatus} and username=#{username} ORDER BY orderid DESC")
    @Transactional
    List<Order> getOrderByState(Integer orderStatus,String username);

    @Select("SELECT COUNT(*) AS row_count FROM `order` where orderStatus = #{orderStatus}")
    Integer countOrderByOrderStatus(String orderStatus);

    @Select("SELECT COUNT(*) AS row_count FROM `order`")
    Integer countOrder();

    @Select("select orderstatus from `order` where orderid = #{orderId}")
    Integer getOrderStatusByorderId(Long orderId);


    @Select("select * from `order` where type = #{type} ORDER BY orderid DESC limit #{pageNum},#{pageSize} ")
    @Transactional
    List<Order> pageByType(@Param("pageNum") Integer pageNum, @Param("pageSize") Integer pageSize, @Param("type") Integer type);


    @Select("select * from `order` where type = #{type} and orderStatus = #{orderStatus} ORDER BY orderid DESC limit #{pageNum},#{pageSize} ")
    @Transactional
    List<Order> pageByOrderStatusAndType(@Param("pageNum") Integer pageNum, @Param("pageSize") Integer pageSize,@Param("orderStatus") Integer orderStatus, @Param("type") Integer type);

}
